#include "cppTutorLog.h"
#include "cppTutorCopyConstructor.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>

using namespace std;

extern CcppTutorLog Log;

int CcppTutorCopyConstructor:: m_nsObjectCount = 0;

CcppTutorCopyConstructor :: CcppTutorCopyConstructor ()
{
    Log << "ENTER : CcppTutorCopyConstructor :: CcppTutorCopyConstructor ()" << endl;
    m_nsObjectCount ++;
    m_ObjectCount ();
    Log << "EXIT  : CcppTutorCopyConstructor :: CcppTutorCopyConstructor ()" << endl;
}

CcppTutorCopyConstructor :: CcppTutorCopyConstructor (int nValue = 0) : m_nValue (nValue)
{
    Log << "ENTER : CcppTutorCopyConstructor :: CcppTutorCopyConstructor (int)" << endl;
    m_nsObjectCount ++;
    m_ObjectCount ();
    Log << "EXIT  : CcppTutorCopyConstructor :: CcppTutorCopyConstructor (int)" << endl;
}

CcppTutorCopyConstructor :: CcppTutorCopyConstructor (char *cpValue) 
{
    Log << "ENTER : CcppTutorCopyConstructor :: CcppTutorCopyConstructor (int)" << endl;

    m_nValue = strlen (cpValue) + 1;
    m_cpValue = new char[m_nValue];
	strncpy (m_cpValue, cpValue, m_nValue);
    m_cpValue [m_nValue - 1] = '\\0';

    m_nsObjectCount ++;
    m_ObjectCount ();
    
    Log << "EXIT  : CcppTutorCopyConstructor :: CcppTutorCopyConstructor (int)" << endl;
}

#ifdef USER_COPY_CONSTRUCTOR
CcppTutorCopyConstructor::CcppTutorCopyConstructor (const CcppTutorCopyConstructor& obj)
{
    Log << "ENTER : CcppTutorCopyConstructor :: CcppTutorCopyConstructor (const CcppTutorCopyConstructor& obj)" << endl;

    m_nValue  = obj.m_nValue;
    if (obj.m_cpValue) {
        m_cpValue = new char [m_nValue];
        strncpy (m_cpValue, obj.m_cpValue, m_nValue);
    } else {
        m_cpValue =  NULL;
    }

    m_nsObjectCount ++;
    m_ObjectCount ();
    
    Log << "EXIT  : CcppTutorCopyConstructor :: CcppTutorCopyConstructor (const CcppTutorCopyConstructor& obj)" << endl;
}
#endif

CcppTutorCopyConstructor :: ~CcppTutorCopyConstructor ()
{
    Log << "ENTER : CcppTutorCopyConstructor :: ~CcppTutorCopyConstructor ()" << endl;

    delete [] m_cpValue;
    m_cpValue = 0;

    m_nsObjectCount --;
    m_ObjectCount ();
    
    Log << "EXIT  : CcppTutorCopyConstructor :: ~CcppTutorCopyConstructor ()" << endl;
}

void CcppTutorCopyConstructor:: m_UseObjectAsArgument (CcppTutorCopyConstructor obj)
{
    Log << "ENTER : CcppTutorCopyConstructor :: m_UseObjectAsArgument (CcppTutorCopyConstructor)" << endl;
    obj.m_DisplayValue ();
    Log << "EXIT  : CcppTutorCopyConstructor :: m_UseObjectAsArgument (CcppTutorCopyConstructor)" << endl;
}

//CcppTutorCopyConstructor CcppTutorCopyConstructor:: m_ReturnObject ()
//{
//    CcppTutorCopyConstructor obj;
//    return obj;
//}

void CcppTutorCopyConstructor :: m_ObjectCount ()
{
    Log << "ENTER : CcppTutorCopyConstructor :: m_ObjectCount ()" << endl;
    cout << "Total : [" << m_nsObjectCount << "]" << " Objects in Scope" << endl;
    Log << "EXIT  : CcppTutorCopyConstructor :: m_ObjectCount ()" << endl;
}

void CcppTutorCopyConstructor :: m_DisplayValue ()
{
    Log << "ENTER : CcppTutorCopyConstructor :: m_DisplayValue ()" << endl;
    Log << m_nValue << endl;
    Log << "EXIT  : CcppTutorCopyConstructor :: m_DisplayValue ()" << endl;
}
